/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.imagepipeline.widget;

import com.facebook.fresco.samples.showcase.annotation.Nullable;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class ResizableStackLayout extends StackLayout implements Component.TouchEventListener {
    private Component mCornerIndicator;
    private boolean mResizing;
    private float mLastX;
    private float mLastY;
    private int mMaximumWidth;
    private int mMaximumHeight;
    private boolean mUpdateMaximumDimensionOnNextSizeChange;

    private @Nullable SizeChangedListener mSizeChangedListener;

    public ResizableStackLayout(Context context) {
        this(context,null);
    }

    public ResizableStackLayout(Context context, AttrSet attrSet) {
        this(context, attrSet,null);
    }

    public ResizableStackLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        setLayoutRefreshedListener(new LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                if (mUpdateMaximumDimensionOnNextSizeChange) {
                    mMaximumWidth = component.getWidth();
                    mMaximumHeight = component.getHeight();
                    mUpdateMaximumDimensionOnNextSizeChange = false;
                }
                if (mSizeChangedListener != null) {
                    mSizeChangedListener.onSizeChanged(component.getWidth(), component.getHeight());
                }
            }
        });

    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
//        if (mCornerIndicator != null){
//            if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN
//                    && touchEvent.getPointerScreenPosition(0).getX() >= mCornerIndicator.getLocationOnScreen()[0]
//                    && touchEvent.getPointerScreenPosition(0).getY() >= mCornerIndicator.getLocationOnScreen()[1]) {
//                ComponentContainer.LayoutConfig layoutParams = getLayoutConfig();
//                layoutParams.width = getWidth();
//                layoutParams.height = getHeight();
//                setLayoutConfig(layoutParams);
//                mResizing = true;
//            }
//        }
//
//        if (!mResizing) {
//            return false;
//        }
        if (touchEvent.getAction() == TouchEvent.POINT_MOVE){
            ComponentContainer.LayoutConfig layoutParams = getLayoutConfig();

            layoutParams.width =Math.max((int) getTouchX(touchEvent,0), getMinWidth());
//            layoutParams.width =
//                    Math.max(Math.min(layoutParams.width, mMaximumWidth), getMinWidth());

            layoutParams.height = Math.max((int) getTouchY(touchEvent,0), getMinHeight());
//            layoutParams.height =
//                    Math.max(Math.min(layoutParams.height, mMaximumHeight), getMinHeight());

            setLayoutConfig(layoutParams);
        }
        mLastX =
                Math.max(
                        Math.min(getTouchX(touchEvent,0), mMaximumWidth), getMinWidth() - mCornerIndicator.getWidth());
        mLastY =
                Math.max(
                        Math.min(getTouchY(touchEvent,0), mMaximumHeight),
                        getMinHeight() - mCornerIndicator.getHeight());
        return true;
    }
    private float getTouchX(TouchEvent touchEvent, int index) {
        float x = 0;
        if (touchEvent.getPointerCount() > index) {
            int[] xy = getLocationOnScreen();
            if (xy != null && xy.length == 2) {
                x = touchEvent.getPointerScreenPosition(index).getX() - xy[0];
            } else {
                x = touchEvent.getPointerPosition(index).getX();
            }
        }
        return x;
    }

    private float getTouchY(TouchEvent touchEvent, int index) {
        float y = 0;
        if (touchEvent.getPointerCount() > index) {
            int[] xy = getLocationOnScreen();
            if (xy != null && xy.length == 2) {
                y = touchEvent.getPointerScreenPosition(index).getY() - xy[1];
            } else {
                y = touchEvent.getPointerPosition(index).getY();
            }
        }
        return y;
    }

    public interface SizeChangedListener {

        void onSizeChanged(int widthPx, int heightPx);
    }



    public void init(Component cornerIndicator) {
        mCornerIndicator = cornerIndicator;

        mMaximumWidth = getWidth();
        mMaximumHeight = getHeight();

        setTouchEventListener(this);
    }

    public void setUpdateMaximumDimensionOnNextSizeChange(
            boolean updateMaximumDimensionOnNextSizeChange) {
        mUpdateMaximumDimensionOnNextSizeChange = updateMaximumDimensionOnNextSizeChange;
    }

    public void setSizeChangedListener(@Nullable SizeChangedListener sizeChangedListener) {
        mSizeChangedListener = sizeChangedListener;
    }
}
